import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from "@/views/layout/index.vue";
import WordDictation from "@/views/layout/wordDictation.vue";
import WordList from "@/views/layout/wordList.vue";
import AlreadyWord from "@/views/layout/alreadyWord.vue";
import Home from "@/views/layout/home.vue";
import WordSetting from "@/views/layout/wordSetting.vue";
import firstPage from '@/views/firstPage'
import login from "@/views/login/index.vue";
import store from "@/store";
import Comment from "@/views/layout/comment.vue";

Vue.use(VueRouter)

const routes = [
  {
    path: '/layout',
    name: 'layout',
    redirect: '/home',
    component: Layout,
    children: [
      {
        path: '/home',
        name: 'home',
        component: Home
      },
      {
        path: '/wordDictation',
        name: 'wordDictation',
        component: WordDictation
      },
      {
        path: '/wordList',
        name: 'wordList',
        component: WordList
      },
      {
        path: '/alreadyWord',
        name: 'alreadyWord',
        component: AlreadyWord
      },
      {
        path: '/wordSetting',
        name: "wordSetting",
        component: WordSetting
      },
      {
        path: '/comment',
        name: 'comment',
        component: Comment
      }
    ]
  },
  {
    path: '/',
    name: 'firstPage',
    component: firstPage
  },
  {
    path: "/login",
    name: 'login',
    component: login
  }


]

const router = new VueRouter({
  routes
})

/**
 * 路由前置守卫
 */
router.beforeEach((to,from , next) =>  {
  const excludePath = ['/login','/']
  //获取token
  const token = store.getters['user/token']
  console.log('token:  '+token)

  //登录页面直接放行
  if (excludePath.includes(to.path)) return  next()

  if (!token) {
    if (from.path !== '/') {
      return next('/')
    }else {
      return next()
    }

  }
  //又token跳转
  next()
})
export default router
